package com.minisoft.goodanchor;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import javafx.scene.effect.SepiaTone;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author JueSu
 * @description: 好主播测试数据
 * @date 2022/5/19 9:48
 */
public class MakeData {

//    static String host = "http://activity-test.paasdev.huafagroup.com";
//    static String host = "http://activity-pre.paas.huafagroup.com";
    static String host = "http://activity.paas.huafagroup.com";
    static String areaUrl = host + "/admin-api/v1/video/getAreas";
    static String accountUrl = host + "/admin-api/v1/video/getAccounts?projectId=";
    static String videoListUrl = host + "/admin-api/v1/video/page";
    static String onUrl = host + "/admin-api/v1/video/on?id=";
    static String actUrl = host + "/admin-api/v1/promotion-activity/partakeAct";
    static String listActUrl = host + "/admin-api/v1/promotion-activity/partakeLogList";

//    static int activityId = 1456;
//    static int activityId = 328;
    static int activityId = 629;
//    int[] projectIds = new int[] {2346};
//    int[] projectIds = new int[] {2346, 2360, 3094, 3433, 3345, 2348};
    int[] projectIds = new int[] {3376};

    /**
     * 参与活动所有视频
     */
    @Test
    public void act() {

        Map<Integer,String> appNameMap = new HashMap<>();
        Map<Integer,String> hfProjectIdMap = new HashMap<>();
        Map<Integer,String> projectNameMap = new HashMap<>();
        JSONArray areas = JSONUtil.parseObj(HttpUtil.get(areaUrl)).getJSONArray("data");
        for (int i = 0; i < areas.size(); i++) {
            JSONObject area = areas.getJSONObject(i);
            JSONArray appList = area.getJSONArray("appList");
            for (int i1 = 0; i1 < appList.size(); i1++) {
                JSONObject app = appList.getJSONObject(i1);
                JSONArray projectList = app.getJSONArray("projectList");
                for (int i2 = 0; i2 < projectList.size(); i2++) {
                    JSONObject pro = projectList.getJSONObject(i2);
                    appNameMap.put(pro.getInt("projectId"),area.getStr("areaName"));
                    hfProjectIdMap.put(pro.getInt("projectId"),pro.getStr("hfProjectId"));
                    projectNameMap.put(pro.getInt("projectId"),pro.getStr("projectName"));
                }
            }
        }

        List<String> list = FileUtil.readUtf8Lines(getClass().getResource("/projectIds.txt"));
        //如果读文件的,要读指定的，则注释掉
//        projectIds = list.stream().mapToInt(Integer::parseInt).distinct().toArray();
        int total = 1;
        //参与活动的项目列表，获取个人账号
        for (Integer projectId : projectIds) {
            String s = HttpUtil.get(accountUrl  + projectId);
            JSONArray data = JSONUtil.parseObj(s).getJSONArray("data");
            for (int i = 0; i < data.size(); i++) {
                JSONObject account = data.getJSONObject(i);
                //过滤掉测试账号
                if (account.getStr("accountName").contains("测试") || account.getStr("accountName").contains("test")) {
                    continue;
                }
                JSONObject params = new JSONObject();
                params.set("accountId",account.getInt("id")).set("pageNo",1).set("pageSize",100);
                JSONArray records = JSONUtil.parseObj(HttpUtil.post(videoListUrl, params.toString())).getJSONObject("data").getJSONArray("records");
                for (int j = 0; j < records.size(); j++) {
                    JSONObject video = records.getJSONObject(j);

                    JSONObject actParams = new JSONObject();
                    actParams.set("avatar",account.getStr("avatarUrl"))
                            .set("nickName",account.getStr("accountName"))
                            .set("phone",account.getStr("mobile"))
                            .set("type",0).set("userJoinType",0).set("activityId",activityId)
                            .set("userId",account.getStr("id"))
                            .set("userName",account.getStr("subjectName"));
                    JSONObject dynamicForm = new JSONObject();
                    dynamicForm.set("videoId",video.getStr("id"))
                            .set("videoUrl",video.getStr("videoUrl"))
                            .set("coverUrl",video.getStr("coverUrl"))
                            .set("videoTitle",video.getStr("videoTitle"))
                            .set("projectName",projectNameMap.get(account.getInt("subjectProjectId")))
                            .set("accountInfo",account.getStr("accountInfo"))
                            .set("userKey",account.getStr("userKey"))
                            .set("shareIcon",account.getStr("shareIcon"))
                            .set("area",appNameMap.get(account.getInt("subjectProjectId")))
                            .set("projectId",hfProjectIdMap.get(account.getInt("subjectProjectId")));
                    JSONArray dynamicForms = new JSONArray();
                    dynamicForms.add(dynamicForm);
                    actParams.set("dynamicForm",dynamicForms);

                    //参与完成
                    Console.log("请求第{}个，参数为{}",total++,actParams);
                    String post = HttpUtil.post(actUrl, actParams.toString());
                    Console.log("结果为{}",post);

                }

            }

        }
    }

    /**
     * 上架活动所有视频
     */
    @Test
    public void on() {

        JSONObject p = new JSONObject();
        p.set("activityId",activityId).set("paTemplateId",22).set("size",999);
        String s = HttpUtil.post(listActUrl, p.toString());
        JSONArray content = JSONUtil.parseObj(s).getJSONObject("data").getJSONArray("content");
        for (int i = 0; i < content.size(); i++) {
            JSONObject actInfo = content.getJSONObject(i);
            Integer id = actInfo.getInt("id");
            String post = HttpUtil.post(onUrl + id,"");
            Console.log("上架视频{}，id={},结果={}",i+1,id,post);
        }

    }
}
